package com.weixin.engine.daily.jvm;

import java.util.concurrent.*;

/**
 * @author wangchunhui
 * @date 2022/9/2 15:43
 */
public class RejectedExecutionHandlerTest {

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                5,
                10,
                5,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(10),
                r -> new Thread(r, "测试线程" + r.hashCode()),
//                new ThreadPoolExecutor.CallerRunsPolicy()
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        r.run();
                    }
                }

        );


        for (int i = 0; i < 100; i++) {
            executor.execute(new Worker(i));
        }
        executor.shutdown();
    }

    static class Worker implements Runnable {
        private final int workerName;

        public Worker(int s) {
            this.workerName = s;
        }

        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            System.out.printf("(%s)= %d is running%n", name, this.workerName);
        }
    }
}
